﻿using System.IO;
using PSE.Updater.Common.Gateway.DataContract;
using PSE.Updater.Common.Providers;
using PSE.Updater.Common.Providers.Interfaces;

namespace PSE.Updater.Server.Engine.Providers.ConfigurationFile
{
    /// <summary>
    /// Classe que representa os detalhes de um determinado arquivo
    /// </summary>
    internal class FileDetails : IFileDetails
    {
        private PackageType? _filePackageType = null;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="productName">Nome do Produto</param>
        public FileDetails(string productName, string rootDirectoryWatcher)
        {
            this.SystemName = productName;
            this.RootDirectoryWatcher = rootDirectoryWatcher;
        }
        /// <summary>
        /// Inicializa as Informações do File
        /// </summary>
        /// <param name="path">Caminho do Arquivo</param>
        public void Initialize(string path, ProviderType providerType)
        {
            FileInfo fileInfo = new FileInfo(path);
            string moduleName = path.Replace(this.RootDirectoryWatcher, "");

            this.FilePath = path;
            this.Size = fileInfo.Length;
            this.ModuleName = fileInfo.Name; 
            this.Version = System.IO.File.GetLastWriteTime(path).ToString();
            this.Status = PackageStatus.Valid;
            this.UpdateDescription = string.Empty;
            this.ProviderType = providerType;
            this._filePackageType = PackageType.Config;
        }

        /// <summary>
        /// Pasta Raiz do Watcher
        /// </summary>        
        public string RootDirectoryWatcher { get; set; }

        /// <summary>
        /// Caminho do Arquivo
        /// </summary>
        public string FilePath { get; set; }
        /// <summary>
        /// Tamanho do Arquivo
        /// </summary>
        public long Size { get; set; }
        /// <summary>
        /// Nome do Produto
        /// </summary>
        public string SystemName { get; set; }
        /// <summary>
        /// Nome do Módulo do Produto
        /// </summary>
        public string ModuleName { get; set; }
        /// <summary>
        /// Versão do Sistema
        /// </summary>
        public string Version { get; set; }
        /// <summary>
        /// Status do Arquivo
        /// </summary>
        public PackageStatus Status { get; set; }
        /// <summary>
        /// Descrição da Atualização
        /// </summary>
        public string UpdateDescription { get; set; }
        /// <summary>
        /// Tipo do Pacote
        /// </summary>
        public PackageType? FilePackageType { get { return this._filePackageType; } }
        /// <summary>
        /// Tipo do Provider
        /// </summary>
        public ProviderType ProviderType { get; private set; }
        /// <summary>
        /// Clona o Objeto
        /// </summary>
        /// <returns>O Objeto clonado</returns>
        public IFileDetails Clone()
        {
            return this.MemberwiseClone() as IFileDetails;
        }
    }
}